home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / yerk 3.66 / Module source / Sort < prev    next >
Text File  |  1994-06-24  |  895b  |  41 lines

  1. \ sort -- Provides a shell sort for arrays of 4-byte elements.
  2. \ 11/30/84  ssg Version 1.0
  3. \ 12/17/84  ssg Made into a module
  4. \                Commented-out sysTimer support
  5. Decimal
  6.  
  7. :Module SortMod
  8.  
  9. \ ( addr1 addr2 -- )  Exchanges 2 4-byte values.
  10. : exchange  2dup @ swap @ rot ! swap ! ;
  11.  
  12. \ Shellsorts an array of n 4-byte elements beginning at baseaddr.
  13. \    comp is the cfa of a word which will compare two elements.
  14. \     Adapted from Software Tools in Pascal, p. 110.
  15. : sort { baseaddr n compWord \ gap jj jg -- }
  16.     n 2/ -> gap
  17.     BEGIN
  18.         gap 0 >
  19.     WHILE
  20.         n 1+  gap 1+
  21.         DO  i gap - -> jj
  22.             BEGIN
  23.                 jj 0 >
  24.             WHILE
  25.                 jj gap + -> jg
  26.                 baseaddr jj 1- 4* +
  27.                 baseaddr jg 1- 4* +        ( addr:e1 addr:e2 )
  28.                 2dup swap @ swap @        ( addr:e1 addr:e2 e1 e2 )
  29.                 exec> compWord 0 <=
  30.                 IF    2drop  0 -> jj
  31.                 ELSE  exchange
  32.                 THEN
  33.                 jj gap - -> jj
  34.             REPEAT
  35.         LOOP
  36.         gap 2/ -> gap
  37.     REPEAT
  38. ;
  39.  
  40. ;Module
  41.